package cn.com.shopec.erp.product.controller;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSON;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.shopec.erp.base.entity.SupplierAccounts;
import cn.com.shopec.erp.base.service.ISupplierAccountsService;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.entity.CatBrand;
import cn.com.shopec.erp.product.service.IBrandService;
import cn.com.shopec.erp.product.service.ICatBrandService;
import cn.com.shopec.erp.product.service.ICommodityService;
import cn.com.shopec.erp.product.vo.BrandVo;
import cn.com.shopec.erp.product.vo.CommodityVo;

@Controller
@RequestMapping("/product/brandmgt")
public class BrandController {

    private static final Log log = LogFactory.getLog(BrandController.class);

    @Resource
    private IBrandService brandService;

    @Resource
    private ICommodityService commodityService;
    
    @Resource
    private ICatBrandService catBrandService;
    
    @Resource
    private ISupplierAccountsService supplierAccountsService;

    /**
     * 
     * @Title: toListBrand
     * @Description: 跳转到品牌列表页面
     * @param brandVo
     * @param map
     * @return
     * @throws Exception
     * @throws
     * @author: duhui
     * @date: 2013-5-27下午01:31:46
     */
    @RequestMapping("/to_list_brand")
    public String toListBrand(BrandVo brandVo, ModelMap map) throws Exception {
        map.put("brand", brandVo);

        return "/product/brand_list";
    }

    /**
     * 
     * @Title: listBrand
     * @Description: 分页查询品牌信息
     * @param brandVo
     * @param pageQuery
     * @param map
     * @return
     * @throws
     * @author: duhui
     * @date: 2013-5-27下午01:33:30
     */
    @RequestMapping("/list_brand")
    @ResponseBody
    public String listBrand(BrandVo brandVo, PageQuery pageQuery, ModelMap map) {
        try {
            PageFinder<BrandVo> pagedBrandList = this.brandService.getPagedBrandList(brandVo, pageQuery);
            map.put("Rows", pagedBrandList.getData());
            map.put("Total", pagedBrandList.getRowCount());

        } catch (Exception e) {
            log.error("listBrand:" + e.getMessage(), e);
        }

        return JsonHelper2.toJson(map);
    }

    /**
     * 
     * @Title: toAddBrand
     * @Description: 跳转到添加品牌页面
     * @return
     * @throws
     * @author: duhui
     * @date: 2013-5-27下午02:41:57
     */
    @RequestMapping("/to_add_brand")
    public String toAddBrand() {
    	
        return "/product/brand_add";
    }

    /**
     * 
     * @Title: addBrand
     * @Description: 添加品牌信息
     * @param brandVo
     * @param map
     * @return
     * @throws Exception
     * @throws
     * @author: duhui
     * @date: 2013-5-27下午01:41:58
     */
    @RequestMapping("/add_brand")
    @ResponseBody
    public String addBrand(BrandVo brandVo, ModelMap map) {
        try {
            this.brandService.addBrand(brandVo);

        } catch (Exception e) {
            log.error("addBrand:" + e.getMessage(), e);
            return SZSCMConstant.FAIL;
        }

        return SZSCMConstant.SUCCESS;
    }

    /**
     * 
     * @Title: toUpdateBrand
     * @Description: 跳转到修改品牌页面
     * @param brandVo
     * @param modelMap
     * @return
     * @throws
     * @author: duhui
     * @date: 2013-5-27下午02:43:02
     */
    @RequestMapping("/to_update_brand")
    public String toUpdateBrand(String brandNo, ModelMap map) {
        try {
            BrandVo brand = this.brandService.getBrand(brandNo);
            map.put("brand", brand);
            
            CatBrand cb = new CatBrand();
            cb.setBrandNo(brandNo);
            List<CatBrand> catBrandList = catBrandService.getCatBrandByBrandNo(cb);
            map.put("catNos", JsonHelper2.toJson(catBrandList));
            map.put("catNo", SZSCMConstant.CAT_TOP_LEVEL);
        } catch (Exception e) {
            log.error("toUpdateBrand:" + e.getMessage(), e);
        }

        return "/product/brand_update";

    }

    /**
     * 
     * @Title: updateBrand
     * @Description: 修改品牌信息
     * @param brandVo
     * @param map
     * @return
     * @throws
     * @author: duhui
     * @date: 2013-5-27下午02:48:51
     */
    @RequestMapping("/update_brand")
    @ResponseBody
    public String updateBrand(BrandVo brandVo, ModelMap map) {
        try {
            this.brandService.updateBrand(brandVo);
        } catch (Exception e) {

            log.error("update_brand:" + e.getMessage(), e);
            return SZSCMConstant.FAIL;
        }

        return SZSCMConstant.SUCCESS;
    }

    /**
     * 
     * @Title: deleteBrand
     * @Description: 删除品牌
     * @param brandVo
     * @return
     * @throws
     * @author: gaoyaxiong
     * @date: 2013-6-6下午20:48:51
     */
    @RequestMapping("deleteBrand")
    @ResponseBody
    public String deleteBrand(BrandVo brandVo) {
        if (!LogicUtil.isNotNull(brandVo)) {
            return "删除品牌有误";
        }
        if (!StringUtil.isExist(brandVo.getBrandNo())) {
            return "删除品牌有误";
        }
        CommodityVo commodityVo = new CommodityVo();
        commodityVo.setBrandNo(brandVo.getBrandNo());
        if (this.commodityService.getCommodityList(commodityVo).size() > 0) {
            return "该品牌已被引用,不可删除";
        }
        SupplierAccounts supplierAccounts = new SupplierAccounts();
        String brandCode = null;
        if(null != this.brandService.getBrand(brandVo.getBrandNo())){
        	brandCode = this.brandService.getBrand(brandVo.getBrandNo()).getNo();
        }
        supplierAccounts.setBrandCode(brandCode);
        if(this.supplierAccountsService.getSupplierAccountsList(supplierAccounts).size() > 0){
        	return "该品牌已被引用,不可删除";
        }
        try {
            this.brandService.delBrand(brandVo.getBrandNo());
            return SZSCMConstant.SUCCESS;
        } catch (Exception e) {
            log.error("deleteBrand:" + e.getMessage(), e);
            return SZSCMConstant.FAIL;
        }
    }

    /**
     * 
     * @Title: getCommodityCatByNo
     * @Description: 验证编号是否重复
     * @param no
     * @return
     * @throws
     * @author: duhui
     * @date: 2013-6-7下午05:26:16
     */
    @RequestMapping("/validationRepeatNo")
    @ResponseBody
    public String validationRepeatNo(BrandVo brandVo) {
        return this.brandService.validationRepeatNo(brandVo);
    }
}
